package tech.aistar.mapper;

import org.apache.ibatis.annotations.*;
import tech.aistar.entity.AppointmentTime;

import java.util.Date;
import java.util.List;

public interface AppointmentTimeMapper {
    @Select("select * from appointment_time")
    List<AppointmentTime> selectAll();

    @Insert("insert into appointment_time(house_id,appointment_date,appointment_state,agent_id,tenant_id,time_quantum)" +
            " value(#{houseId},#{appointmentDate},#{appointmentState},#{agentId},#{tenantId},#{timeQuantum})")
    void insert(AppointmentTime appointmentTime);

    @Update("update appointment_time set tenant_id=#{tenantId},house_id=${houseId}," +
            "appointment_date=#{appointmentDate},time_quantum=#{timeQuantum},appointment_state=${appointmentState}," +
            "agent_id=#{agentId} where id=#{Id}")
    void  updateByPrimaryKey(AppointmentTime appointmentTime);

    @Delete("delete from appointment_time where id=#{Id}")
    void deleteByPrimaryKey(Integer id);

    @Select("select * from appointment_time where id=#{Id}")
    AppointmentTime findById(Integer integer);

    @Select("select * from appointment_time where house_id=#{house_id} and appointment_state=#{appointment_state}")
    List<AppointmentTime> findByhouseId(@Param("house_id") Integer house_id,@Param("appointment_state") String appointment_state);


    @Select("select appointment_date,time_quantum from appointment_time where id=#{Id}")
    AppointmentTime findTimeById(Integer integer);

    @Select("select * from appointment_time where appointment_date=#{appointmentDate} and agent_id=#{agentId}")
    List<AppointmentTime> selectAgentByAppointmentDate(@Param("appointmentDate") Date appointmentDate, @Param("agentId") Integer agentId);


    @Select("select * from appointment_time where tenant_id=#{tenantId}")
    List<AppointmentTime> selectByTenantId(Integer tenantId);



    @Delete("delete from appointment_time where id=#{id}")
    int deleteById(Integer id);


    @Select("select * from appointment_time where appointment_date=#{appointmentDate} and time_quantum=#{timeQuantum} and agent_id=#{agentId} and appointment_state='预约成功'")
    AppointmentTime selectByAppointmentDate(@Param("appointmentDate") Date appointmentDate, @Param("timeQuantum") String timeQuantum, @Param("agentId") Integer agentId);


    @Select("select * from appointment_time where appointment_date=#{appointmentDate} and time_quantum=#{timeQuantum} and tenant_id=#{tenantId} and appointment_state<>'预约失败'")
    AppointmentTime selectByAppointmentDates(@Param("appointmentDate") Date appointmentDate, @Param("timeQuantum") String timeQuantum, @Param("tenantId") Integer tenantId);

    @Select("select * from appointment_time where id=#{id}")
    AppointmentTime selectById(Integer id);

    @Update("update appointment_time set appointment_date=#{date},time_quantum=#{time},appointment_state='审核中' where id=#{id}")
    int  updateById(@Param("date") Date appointmentDate, @Param("time") String timeQuantum, @Param("id") Integer id);



    @Select("select * from appointment_time where agent_id=#{agentId}")
    List<AppointmentTime> selectByAgentId(Integer agentId);

    @Update("update appointment_time set appointment_state='预约成功' where id=#{id}")
    int  updateStateById(Integer id);

    @Update("update appointment_time set appointment_state='预约失败' where id=#{id}")
    int  updateStatefById(Integer id);

    @Update("update appointment_time set appointment_state='预约失败' where appointment_date=#{appointmentDate} and time_quantum=#{timeQuantum} and agent_id=#{agentId} and id<>#{id}")
    int  updateStateBy(AppointmentTime appointmentTime);

    @Select("select * from appointment_time where appointment_date=#{appointmentDate} and time_quantum=#{timeQuantum} and agent_id=#{agentId} and id<>#{id}")
    List<AppointmentTime> selectBy(AppointmentTime appointmentTime);

}
